/**
 * 
 */
package com.depot.org.organization.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.base.KeyGenerator;
import com.depot.domains.Organization;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SortTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.util.StringUtils;

public class OrganizationDao
{
	/**
	 * 组织机构_list
	 * 
	 * @param env
	 * @param domain
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public static List<HashMap<String, String>> select(Environment env, Organization domain, BaseActionSupport action, String rolecode) throws SQLException
	{
		DbParameter param = new DbParameter("ORGANIZATION T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.ORG_NAME,");
		sb.append("T1.ORG_CODE,");
		sb.append("T1.TELPHONE,");
		sb.append("T1.MOBILE,");
		sb.append("T1.EMAIL,");
		sb.append("T1.STATUS,");
		sb.append("T1.CREATE_USER,");
		sb.append("T1.CREATE_DATE,");
		sb.append("T1.UPDATE_USER,");
		sb.append("T1.UPDATE_DATE");
		param.setSelectFields(sb.toString());

		if (StringUtils.isNoEmpty(domain.getSearchOrgName()))
		{
			param.setWhereParams("T1.ORG_NAME", domain.getSearchOrgName(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchOrgCode()))
		{
			param.setWhereParams("T1.ORG_CODE", domain.getSearchOrgCode(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchTelphone()))
		{
			param.setWhereParams("T1.TELPHONE", domain.getSearchTelphone(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchMobile()))
		{
			param.setWhereParams("T1.MOBILE", domain.getSearchMobile(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchEmail()))
		{
			param.setWhereParams("T1.EMAIL", domain.getSearchEmail(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}

		if ("ordinary".equals(rolecode))
		{
			//普通角色代码
			param.setWhereParams("T1.ID", env.getLoginUser().getUserOrgId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			return DbTemplates.select(env.getConnection(), param);
		}
		else
		{
			//海关或者管理员角色
			if (action.getSys_p_sort_field().equals(""))
			{
				action.setSys_p_sort_field("T1.ID");
				action.setSys_p_sort_method(SortTypes.DESC);
			}
			param.setOrderParams(action.getSys_p_sort_field(), action.getSys_p_sort_method());
			return DbTemplates.select(env.getConnection(), param, action);
		}
	}

	public static HashMap<String, String> doShow(Environment env, Organization domain) throws SQLException
	{
		DbParameter param = new DbParameter("ORGANIZATION T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.ORG_NAME,");
		sb.append("T1.ORG_CODE,");
		sb.append("T1.TELPHONE,");
		sb.append("T1.MOBILE,");
		sb.append("T1.EMAIL,");
		sb.append("T1.STATUS");
		param.setSelectFields(sb.toString());
		param.setWhereParams("T1.ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		return DbTemplates.select(env.getConnection(), param).get(0);
	}

	public static String doSave(Environment env, Organization domain) throws SQLException
	{
		DbParameter param = null;
		if (StringUtils.isEmpty(domain.getId()))
		{// ID为空 新增

			String[] fieldsName = { "ORG_NAME" };
			String[] fieldsValue = { domain.getOrgName() };

			Boolean exclude = DbTemplates.nameFlag(env.getConnection(), null, fieldsName, fieldsValue, "ORGANIZATION");

			String[] fieldsName1 = { "ORG_CODE" };
			String[] fieldsValue1 = { domain.getOrgCode() };

			Boolean exclude1 = DbTemplates.nameFlag(env.getConnection(), null, fieldsName1, fieldsValue1, "ORGANIZATION");

			if (!(exclude))
			{
				return "2";
			}
			if (!(exclude1))
			{
				return "2";
			}
			String oid = KeyGenerator.getInstance().getKeyId(env.getConnection());
			param = new DbParameter("ORGANIZATION T1");
			param.setParams("ID", oid, SqlTypes.CHAR);
			param.setParams("ORG_NAME", domain.getOrgName(), SqlTypes.VARCHAR);
			param.setParams("ORG_CODE", domain.getOrgCode(), SqlTypes.VARCHAR);
			param.setParams("TELPHONE", domain.getTelphone(), SqlTypes.VARCHAR);
			param.setParams("MOBILE", domain.getMobile(), SqlTypes.VARCHAR);
			param.setParams("EMAIL", domain.getEmail(), SqlTypes.VARCHAR);
			param.setParams("STATUS", domain.getStatus() == null ? "0" : domain.getStatus(), SqlTypes.CHAR);

			param.setParams("CREATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("CREATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("CREATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
			if (DbTemplates.insert(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
		else
		{// ID不为空 修改
			String[] idkey = { domain.getId(), "ID" };
			String[] fieldsName = { "ORG_NAME" };
			String[] fieldsValue = { domain.getOrgName() };

			Boolean exclude = DbTemplates.nameFlag(env.getConnection(), idkey, fieldsName, fieldsValue, "ORGANIZATION");

			String[] fieldsName1 = { "ORG_CODE" };
			String[] fieldsValue1 = { domain.getOrgCode() };

			Boolean exclude1 = DbTemplates.nameFlag(env.getConnection(), idkey, fieldsName1, fieldsValue1, "ORGANIZATION");

			if (!(exclude))
			{
				return "2";
			}
			if (!(exclude1))
			{
				return "2";
			}

			param = new DbParameter("ORGANIZATION T1");
			param.setParams("ORG_NAME", domain.getOrgName(), SqlTypes.VARCHAR);
			param.setParams("ORG_CODE", domain.getOrgCode(), SqlTypes.VARCHAR);
			param.setParams("TELPHONE", domain.getTelphone(), SqlTypes.VARCHAR);
			param.setParams("MOBILE", domain.getMobile(), SqlTypes.VARCHAR);
			param.setParams("EMAIL", domain.getEmail(), SqlTypes.VARCHAR);
			param.setParams("STATUS", domain.getStatus() == null ? "0" : domain.getStatus(), SqlTypes.CHAR);

			param.setParams("CREATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("CREATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("CREATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setWhereParams("T1.ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			if (DbTemplates.update(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
	}

	/**
	 * 组织机构_delete
	 * 
	 * @param env
	 * @param domain
	 * @throws Exception
	 */
	public static String doDelete(Environment env, Organization domain) throws SQLException
	{

		if (domain.getIds() == null)
		{
			return "0";
		}
		deleteuser(env, domain);
		
		DbParameter param = null;
		param = new DbParameter("ORGANIZATION T1");
		param.setWhereParams("T1.ID", param.getInSQL(domain.getIds()), SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.IN);

		if (DbTemplates.delete(env.getConnection(), param))
		{
			return "1";
		}
		else
		{
			return "0";
		}
	}

	private static void deleteuser(Environment env, Organization domain) throws SQLException
	{

		DbParameter param = null;
		param = new DbParameter("USERS T1");
		param.setParams("DEP_ID", "", SqlTypes.CHAR);
		param.setParams("ORG_ID", "", SqlTypes.CHAR);
		param.setParams("STATUS", "3", SqlTypes.CHAR);
		param.setWhereParams("T1.ORG_ID", param.getInSQL(domain.getIds()), SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.IN);
		DbTemplates.update(env.getConnection(), param);
	}
}